package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import qlsl.androiddesign.activity.subactivity.CarActivity;
import qlsl.androiddesign.activity.subactivity.CarDetailActivity;
import qlsl.androiddesign.adapter.subadapter.UsedCarAdapter;
import qlsl.androiddesign.entity.commonentity.Pager;
import qlsl.androiddesign.entity.otherentity.CarBrand;
import qlsl.androiddesign.entity.otherentity.CarDetail;
import qlsl.androiddesign.http.service.subservice.CarService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshListView;
import qlsl.androiddesign.listener.OnPullRefreshListener;
import qlsl.androiddesign.popupwindow.subwindow.BrandPopupWindow;
import qlsl.androiddesign.popupwindow.subwindow.ProvincePopupWindow;
import qlsl.androiddesign.view.baseview.FunctionView;

/**
 * 买车列表页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class CarView extends FunctionView<CarActivity> {

	private PullToRefreshListView refreshView;

	private BrandPopupWindow brandWindow;

	private ProvincePopupWindow provinceWindow;

	private List<CarDetail> list = new ArrayList<CarDetail>();

	private Pager pager;

	public CarView(CarActivity activity) {
		super(activity);
		setContentView(R.layout.activity_car);
	}

	protected void initView(View view) {
		int position = activity.getIntent().getIntExtra("position", 0);
		String[] texts = new String[] { "小挖", "小挖", "中挖", "中挖", "大挖" };
		setTitle(texts[position]);
		refreshView = findViewById(R.id.refreshListView);
		brandWindow = new BrandPopupWindow(activity);
		provinceWindow = new ProvincePopupWindow(activity);
	}

	protected void initData() {
		int position = activity.getIntent().getIntExtra("position", 0);
		String[] types = new String[] { "小挖6吨以下", "小挖6-9吨", "中挖10-19吨", "中挖20-29吨", "大挖30吨以上" };
		CarService.queryBrandList(this, activity);
		queryCarList(null, null, types[position], 1);
	}

	protected void initListener() {
		refreshView.setOnRefreshListener(refreshListener);
	}

	@SuppressWarnings("unchecked")
	public <T> void showData(T... t) {
		if (t[0] instanceof HashMap) {
			HashMap<String, Object> map = (HashMap<String, Object>) t[0];
			List<CarDetail> list_result = (List<CarDetail>) map.get("list");
			pager = (Pager) map.get("pager");
			if (pager.getPageNo() == 1) {
				resetList(list_result);
			} else {
				list.addAll(list_result);
			}
			notifyDataSetChanged();
		} else if (t[0] instanceof List) {
			List<CarBrand> list = (List<CarBrand>) t[0];
			brandWindow.setGridViewData(list);
		}

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_guide1:
			doClickGuideView(view, 0);
			break;
		case R.id.tv_guide2:
			doClickGuideView(view, 1);
			break;
		case R.id.list_item:
			doClickListItem(view);
			break;
		case R.id.btn_ok:
			doClickOKView(1);
			break;
		}
	}

	private void doClickGuideView(View view, int pos) {
		if (pos == 0) {
			findViewById(R.id.tv_guide1).setActivated(true);
			findViewById(R.id.tv_guide2).setActivated(false);
			brandWindow.showAsDropDown((ViewGroup) view.getParent());
			provinceWindow.dismiss();
		} else if (pos == 1) {
			findViewById(R.id.tv_guide1).setActivated(false);
			findViewById(R.id.tv_guide2).setActivated(true);
			brandWindow.dismiss();
			provinceWindow.showAsDropDown((ViewGroup) view.getParent());
		}
	}

	private void queryCarList(String brands, String province, String type, int page) {
		CarService.queryUsedCarList(brands, province, type, page, this, activity);
	}

	private void doClickListItem(View view) {
		int position = refreshView.getRefreshableView().getPositionForView(view);
		CarDetail item = (CarDetail) refreshView.getAdapter().getItem(position);
		startActivity(item, CarDetailActivity.class);
	}

	private void doClickOKView(int page) {
		brandWindow.dismiss();
		provinceWindow.dismiss();
		CarBrand brand = brandWindow.getSelectItem();
		String brandname = null;
		String province = provinceWindow.getSelectItem();
		if (province.equals("不限")) {
			province = null;
		}
		if (brand != null) {
			if (brand.getName().equals("不限")) {
				brandname = null;
			} else {
				brandname = brand.getName();
			}
		}
		int position = activity.getIntent().getIntExtra("position", 0);
		String[] types = new String[] { "小挖6吨以下", "小挖6-9吨", "中挖10-19吨", "中挖20-29吨", "大挖30吨以上" };
		queryCarList(brandname, province, types[position], page);
	}

	private void resetList(List<CarDetail> list_result) {
		list.clear();
		list.addAll(list_result);
	}

	private void notifyDataSetChanged() {
		BaseAdapter adapter = (BaseAdapter) refreshView.getBaseAdapter();
		if (adapter == null) {
			adapter = new UsedCarAdapter(activity, list);
			refreshView.setAdapter(adapter);
		} else {
			adapter.notifyDataSetChanged();
		}
		refreshView.onRefreshComplete();
	}

	private OnPullRefreshListener<ListView> refreshListener = new OnPullRefreshListener<ListView>() {

		public void onPullDown() {
			doClickOKView(1);
		}

		public void onPullUp() {
			super.onPullUp(refreshView, pager);
		}

		public void nextPager() {
			doClickOKView(pager.getPageNo() + 1);
		}
	};
}
